package xlcao.sohutv.adapter;

import java.util.ArrayList;

import xlcao.sohutv.R;
import xlcao.sohutv.bean.TVInfo;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;


public class TvlistAdapter extends BaseAdapter {
	private Activity mContext;
	
	private int mLayoutId;
	private ArrayList<TVInfo> mTvList;
	private int mSelectemItem;
	public TvlistAdapter(Activity context) {
		mContext = context;
		mLayoutId = R.layout.list_item;
	}
	
	public TvlistAdapter(Activity context, int layoutId) {
		mContext = context;
		mLayoutId = layoutId;
	}

	public void setTVInfoList(ArrayList<TVInfo> list) {
		mTvList = list;
	}
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        if (convertView == null) {
        	convertView = LayoutInflater.from(mContext.getApplicationContext()).inflate(mLayoutId, null);
           // convertView = inflater.inflate(mLayoutId, null);
        }

        ((TextView)convertView.findViewById(R.id.list_title)).setText(((TVInfo)getItem(position)).getTitle());
        Bitmap bm = ((TVInfo)getItem(position)).getIcon();
        if ( bm != null) {
        	Drawable icon = (Drawable)new BitmapDrawable(bm);
        	icon.setBounds(0,0,30,30);
        	((TextView) convertView).setCompoundDrawables(icon, null,null,null);
        }
        else {
        	Drawable icon = mContext.getResources().getDrawable(R.drawable.icon);
        	icon.setBounds(0,0,30,30);
        	((TextView) convertView).setCompoundDrawables(icon, null,null,null);
        }
        ((TextView) convertView).setCompoundDrawablePadding(8);
        	//convertView.findViewById(R.id.tv_icon)).setImageResource(n);
        
        /*if (position == mSelectemItem) {
        	convertView.setBackgroundColor(Color.RED);  
        } else {
        	convertView.setBackgroundColor(Color.TRANSPARENT);  
        }*/
        return convertView;
    }

	@Override
	public int getCount() {
		return mTvList.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return mTvList.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}
	
	public void setSelectedItem(int item) {
		mSelectemItem = item;
	}
}